/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.controls;

import wwcontrolserver.datarecorders.WWControlServerLogger;

/**
 *
 * @author Michael Ferry
 */
public abstract class AbstractSetPointHeaterControl extends AbstractControl {

    
    protected SetPointHeaterControlState CurrentState;
    
    public AbstractSetPointHeaterControl(SetPointHeaterControlState InitialState, WWControlServerLogger WWCSLog){
        super(InitialState, WWCSLog);
        this.CurrentState = InitialState;
    }
    
    protected abstract double GetCurrentTemperature() throws Exception;
    protected abstract void SetHeaterState(boolean HeaterState) throws Exception;
    protected abstract boolean GetHeaterState() throws Exception;
    
    @Override
    public void SetControlState(AbstractControlState State) throws Exception{
        CurrentState.UpdateControlState(State);
        UpdateSetPointHeaterControlState();     
    }
    protected final void UpdateSetPointHeaterControlState() {
        //check if the control has been activated or deactivated
        if (CurrentState.ControlActivation) {
            WWCSLog.RecordLogEntry("Control active for heater: " + CurrentState.ControlName);
            try {               
                CarryOutControlFunction();          
            } catch (Exception e) {
                //e.printStackTrace();
                WWCSLog.RecordExceptionLogEntry(e);
            }
        } else {
            WWCSLog.RecordLogEntry("Control inactive - heater: " + CurrentState.ControlName + " turned off");
            try {
                SetHeaterState(false);                
            } catch (Exception e) {
                WWCSLog.RecordExceptionLogEntry(e);
            }
        }
    }
    
    @Override
    public SetPointHeaterControlState GetControlState(){
        return CurrentState;
    }
    
    @Override
    public void NotifyControl() {
        //Only actuate control if so desired
        String NotificationString = "Control Notified " + this.GetControlName();
        System.out.println(NotificationString);
        WWCSLog.RecordLogEntry(NotificationString);
        if (CurrentState.ControlActivation) {
            try {
                CarryOutControlFunction();
            } catch (Exception e) {
                //e.printStackTrace();
               WWCSLog.RecordExceptionLogEntry(e);
            }
        }
    }
    @Override
    public void ShutdownControl(){
        CurrentState.SetOffState();
        UpdateSetPointHeaterControlState(); 
        
    }

    
    private void CarryOutControlFunction() throws Exception {
        double CurrentTemp = GetCurrentTemperature();
        if (CurrentTemp < CurrentState.SetPointTemp - CurrentState.SetPointRange) {
            WWCSLog.RecordLogEntry("Turning on heater: " + CurrentState.ControlName); 
            //System.out.println("Turning on heater: "+ OutputRelayNumber);
            SetHeaterState(true);
        } //If the barrel temperature is above the TempSet point minus the TempSetRange turn the heater off
        else if (CurrentTemp > CurrentState.SetPointTemp + CurrentState.SetPointRange) {
            //Determine if the control is beyond the warning point
            if(CurrentTemp > CurrentState.GetWarningPointTemp()){
                WWCSLog.RecordWarning("Heater " + CurrentState.ControlName + " has exceeded the warning point temperature");
            }
            //System.out.println("Turning off heater: "+ OutputRelayNumber);
            WWCSLog.RecordLogEntry("Turning off heater: " + CurrentState.ControlName); 
            SetHeaterState(false);
        } else {
            //Do nothing, heater temperature within range
        }
    }

}
